package com.magic.smartcity.view.more.bus

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.magic.smartcity.R
import com.magic.smartcity.databinding.FragmentBusOrderNotPaidBinding
import com.magic.smartcity.databinding.FragmentBusOrderPaidBinding
import com.magic.smartcity.view.more.adapters.BusOrderListAdapter
import com.magic.smartcity.view.more.viewmodels.BusViewModel

class BusOrderNotPaidFragment : Fragment() {
    private lateinit var binding: FragmentBusOrderNotPaidBinding

    private val viewModel by lazy {
        ViewModelProvider(this).get(BusViewModel::class.java)
    }

    override fun onResume() {
        super.onResume()
        viewModel.getOrderList()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentBusOrderNotPaidBinding.inflate(layoutInflater)
        initView()
        return binding.root
    }

    private fun initView() {
        binding.orderList.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        viewModel.busOrderList.observe(viewLifecycleOwner) {
            val list = it.filter { data -> data.status == 0 }.toMutableList()
            binding.orderList.adapter = BusOrderListAdapter(list, viewModel)
        }
        viewModel.isPayOrderOk.observe(viewLifecycleOwner) {
            if (it) {
                Toast.makeText(requireContext(), "支付成功", Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(requireContext(), "支付失败", Toast.LENGTH_SHORT).show()
            }
        }
    }

}